package bancosys.tec.rtm.impl.exception.dao;

import java.util.List;

import jmine.tec.purge.api.PurgeHelper;
import jmine.tec.purge.api.Purger;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BeanNotFoundException;
import bancosys.tec.rtm.impl.exception.domain.ExceptionRecord;
import bancosys.tec.utils.date.Date;

/**
 * Expurgador do RTM.
 * 
 * @author Marcio Ribeiro (mmr)
 * @created Aug 18, 2011
 */
public class RtmPurger extends BaseDAO<ExceptionRecord> implements Purger {

    /**
     * {@inheritDoc}
     */
    public int purge(Date maxDate, int maxWindowSize) {
        List<Long> ids = PurgeHelper.getIds(createCriteria(), "timestamp", maxDate, maxWindowSize);

        // XXX (mmr) : nao da para usar apagar com bulk delete por causa do @CollectionOfElements em context :/
        int i = 0;
        for (; i < ids.size(); ++i) {
            try {
                findByPk(ids.get(i)).getPersister().remove();
            } catch (BeanNotFoundException e) {
                throw new RuntimeException(e);
            }
        }

        return i;
    }

    /**
     * {@inheritDoc}
     */
    public int countItemsToPurge(Date maxDate) {
        return PurgeHelper.countItemsToPurge(createCriteria(), "timestamp", maxDate);
    }
}
